﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ConrtolLibrary
{
    public enum StatusLightMode
    {
        OK,
        OFFLINE,
        ERROR,
        ARMED,
        LAUNCED,
    }

    public partial class StatusLight : UserControl
    {

        private float _lineWidth = 1.0f;
        private int _padding = 5;
        private int _textSize = 18;
        private string _text = "";
        private StatusLightMode _status;
        private Timer ArmedTimer = new Timer();
        private bool ArmedFlashing = false;

        public StatusLightMode Status
        {
            get { return _status; }
            set { _status = value; Invalidate(); }
        }

        public int TextSize
        { 
            get { return _textSize; }
            set { _textSize = Math.Max(value,4); Invalidate(); }
        }


        public float LineWidth
        {
            get { return _lineWidth; }
            set { _lineWidth = value; Invalidate(); }
        }

        public int BorderPadding
        {
            get { return _padding; }
            set { _padding = value; Invalidate(); }
        }

        public string TextValue
        {
            get { return _text; }
            set { _text = value; Invalidate(); }
        }

        public StatusLight()
        {
            InitializeComponent();
            ArmedTimer.Interval = 1000;
            ArmedTimer.Tick +=new EventHandler(ArmedTimer_Tick);
        }

        private void ArmedTimer_Tick(object sender, EventArgs e)
        {
            Invalidate();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            Rectangle rect = new Rectangle(_padding, _padding, Width - _padding * 2, Height - _padding * 2);
            
            //draw background + text
            if (Status != StatusLightMode.ARMED && ArmedTimer.Enabled) ArmedTimer.Stop();

            switch (Status)
            {
                case StatusLightMode.OK:
                    e.Graphics.FillRectangle(new SolidBrush(Color.Lime), rect);
                    drawText(e.Graphics, "OK", _textSize, Color.Black);
                    break;
                case StatusLightMode.OFFLINE:
                    e.Graphics.FillRectangle(new SolidBrush(BackColor), rect);
                    drawText(e.Graphics, "OFF", _textSize, ForeColor);
                    break;
                case StatusLightMode.ERROR:
                    e.Graphics.FillRectangle(new SolidBrush(Color.Red), rect);
                    drawText(e.Graphics, "ERR", _textSize, Color.Black);
                    break;
                case StatusLightMode.LAUNCED:
                    e.Graphics.FillRectangle(new SolidBrush(Color.Gray), rect);
                    drawText(e.Graphics, "AWAY", _textSize-3, Color.Black);
                    break;
                case StatusLightMode.ARMED:
                    if(!ArmedTimer.Enabled) ArmedTimer.Start();

                    if( ArmedFlashing == false )
                    {
                        e.Graphics.FillRectangle(new SolidBrush(Color.Red), rect);
                        drawText(e.Graphics, "ARMED", _textSize - 4, Color.Black);
                        ArmedFlashing = true;
                    }
                    else
                    {
                        e.Graphics.FillRectangle(new SolidBrush(BackColor), rect);
                        drawText(e.Graphics, "ARMED", _textSize - 4, ForeColor);
                        ArmedFlashing = false;
                    }

                    
                    
                    break;

                default:
                    break;
                    
            }

            //draw border
            using (Pen pen = new Pen(ForeColor, _lineWidth))
            {
                e.Graphics.DrawRectangle(pen, rect);
            }
            
        }

        private void drawText(Graphics g, string str, int size, Color textColor)
        {
            StringFormat strFormat = new StringFormat();
            strFormat.Alignment = StringAlignment.Center;
            strFormat.LineAlignment = StringAlignment.Center;
            g.DrawString(str, new Font("Tahoma", size), new SolidBrush(textColor),
                new RectangleF(0, 0, Width, Height), strFormat);
        }
    }
}
